package com.qingneng.web;

import java.util.Date;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.service.sdk.IContractInfoService;
import com.qingneng.vo.ContractInfo;
import com.tbfs.common.BaseAction;
import com.tbfs.security.CurrentUser;
import com.tbfs.util.UtilTool;

public class SaveContractInfoAction extends BaseAction implements ModelDriven<ContractInfo>{
	
	private static final Logger logger = Logger.getLogger(SaveContractInfoAction.class);
	
	private ContractInfo contractInfo = new ContractInfo();
	

	public ContractInfo getContractInfo() {
		return contractInfo;
	}

	public void setContractInfo(ContractInfo contractInfo) {
		this.contractInfo = contractInfo;
	}

	
	private String message = null;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	// init service
	private IContractInfoService contractInfoService;
	
	// 构造函数
	public SaveContractInfoAction(IContractInfoService contractInfoService) {
		this.contractInfoService = contractInfoService;
	}

	public SaveContractInfoAction() {

	}
	
	
	/**
	 * save contractinfo data 
	 * @return
	 */
	public String saveData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin save contractinfo +++");
		}
		logger.info("contractName:"+contractInfo.getContractName());
		
		logger.info("id:"+contractInfo.getId());
		
		try {
			int id = contractInfo.getId();
			
			if(id>0){
				logger.info("+++begin execute modifyContractInfo id="+contractInfo.getId()+"-----time-----"+UtilTool.formatDate(new Date()));
				contractInfoService.modifyContractInfo(contractInfo);
				logger.info("+++begin end modifyContractInfo id="+contractInfo.getId()+"-----time-----"+UtilTool.formatDate(new Date()));
				contractInfo.setMessage("success");
				
			}else{
				contractInfo.setContractStatus(0);
				contractInfo.setCreatePerson(CurrentUser.getUser().getUseraccount());
				contractInfoService.addContractInfo(contractInfo);	
				contractInfo.setMessage("success");
			}
			
			
		} catch (Exception e) {
			contractInfo.setMessage("添加失败，请检查输入项目");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end save contractinfo +++");
		}
		return Action.SUCCESS;
	}
	
	public String destoryData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin delete contractinfo +++");
		}
		
		logger.info("id:"+contractInfo.getId());
		
		try {
		
			contractInfoService.destoryContractInfo(contractInfo);
			
			contractInfo.setMessage("success");
			
		} catch (Exception e) {
			contractInfo.setMessage("添加失败，请检查输入项目");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end delete contractinfo +++");
		}
		return Action.SUCCESS;
	}
	/**
	 * 审核
	 * @return
	 */
	public String audit(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin  contractInfo audit +++");
		}
		try {
			logger.info("contractInfo_id:"+contractInfo.getId());
			contractInfo.setAuditTime(UtilTool.formatDate(new Date()));
			//从session获取
			contractInfo.setAuditPerson(CurrentUser.getUser().getUseraccount());
			
			contractInfoService.modifyContractInfo(contractInfo);
			//审核成功
			contractInfo.setMessage("success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++ end  contractInfo audit +++");
		}
		return Action.SUCCESS;
	}
	
	
	/**
	 * 签订
	 * @return
	 */
	public String signContract(){
		//签订状态
		contractInfo.setContractStatus(3);
		try {
			contractInfoService.modifyContractInfo(contractInfo);
			contractInfo.setMessage("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}

	public ContractInfo getModel() {
		// TODO Auto-generated method stub
		return contractInfo;
	}

}
